package src.com.hpu.tools;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import android.R.integer;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Cookie;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 网络请求的工具类，主要对OkHttp这个框架进行二次封装，
 * 目前只有两个Get的方法
 * @author Administrator
 *
 */
public class OkHttpTools {

	private static OkHttpClient okHttpClient = null;
	
	private static OkHttpClient getInstance() {
		return okHttpClient == null ? new OkHttpClient() : okHttpClient;
	}

	/**
	 * GET请求，callback是回调接口
	 * @param url
	 * @param callback
	 */
	public static void get(String url, Callback callback) {
		okHttpClient = getInstance();
		Request request = new Request.Builder().url(url).build();
		okHttpClient.newCall(request).enqueue(callback);
	}

	/**
	 * 携带Cookie的GET请求，callback是回调接口
	 * @param url
	 * @param callback
	 */
	public static void getByCookie(String url, String cookie, Callback callback) {
		okHttpClient = getInstance();
		Request request = new Request.Builder().url(url).addHeader("Cookie", cookie).build();
		okHttpClient.newCall(request).enqueue(callback);
	}
	
	public static void post(String url,Callback callback,String[] keys,String[] values){
		okHttpClient = getInstance();
		FormBody.Builder builder = new FormBody.Builder();  
		for(int i=0;i<keys.length;i++){
			builder.add(keys[i],values[i]);  
		}
		
		RequestBody formBody = builder.build();  
		Request request = new Request.Builder()  
	    .url(url)  
	    .post(formBody)  
	    .build();  
		okHttpClient.newCall(request).enqueue(callback);
	}
	
	public static void upLoadFile(String url,String cookie, HashMap<String, Object> paramsMap, Callback callBack) {
        try {
            MultipartBody.Builder builder = new MultipartBody.Builder();
            //设置类型
            builder.setType(MultipartBody.FORM);
            
            //追加参数
            for (String key : paramsMap.keySet()) {
                Object object = paramsMap.get(key);
                if (!(object instanceof File)) {
                    builder.addFormDataPart(key, object.toString());
                } else {
                    File file = (File) object;
                    builder.addFormDataPart(key, file.getName(), RequestBody.create(MediaType.parse("image/png"), file));
                }
            }
            //创建RequestBody
            RequestBody body = builder.build();
            //创建Request
            final Request request = new Request.Builder().url(url).post(body).addHeader("Cookie", cookie).build();
            //单独设置参数 比如读取超时时间
            final Call call = getInstance().newBuilder().writeTimeout(50, TimeUnit.SECONDS).build().newCall(request);
            call.enqueue(callBack);
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
	
	public static void postByCookie(String url,String cookie,Callback callback,String[] keys,String[] values){
		okHttpClient = getInstance();
		FormBody.Builder builder = new FormBody.Builder();  
		for(int i=0;i<keys.length;i++){
			builder.add(keys[i],values[i]);  
		}
		
		RequestBody formBody = builder.build();  
		Request request = new Request.Builder()  
	    .url(url)  
	    .post(formBody)
	    .addHeader("Cookie", cookie)
	    .build();  
		okHttpClient.newCall(request).enqueue(callback);
	}
}
